<template>
  <el-dialog :visible="showdialog" title="新增员工" @close="cancel">
    <!-- 表单 -->
    <el-form ref="addForm" label-width="120px" :model="adeptForm" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="adeptForm.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="adeptForm.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="adeptForm.timeOfEntry" type="date" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="adeptForm.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="adeptForm.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="adeptForm.departmentName" style="width:50%" placeholder="请选择部门" @focus="treeShow" />
        <!-- 放置树形数据--部门 -->
        <el-tree v-if="showTree" v-loading="loading" :data="treeNode" :props="defaultProps" default-expand-all @node-click="handleNodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="adeptForm.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="cancel">取消</el-button>
          <el-button type="primary" size="small" @click="addemployee">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
import { addemployee } from '@/api/employees'
export default {
  props: {
    showdialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 加载进度条
      loading: false,
      // 树形结构部门
      treeNode: [],
      // 是否显示树形结构
      showTree: false,
      // 树形结构显示规则
      defaultProps: {
        label: 'name'
      },
      // 聘用形式数据
      hireType: EmployeeEnum.hireType,
      // 表单数据
      adeptForm: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        workNumber: '',
        correctionTime: '',
        departmentName: '',
        formOfEmployment: ''
      },
      // 校验规则
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 取消
    cancel() {
      this.$refs.addForm.resetFields() // 清空表单
      this.showTree = false // 关闭树形
      this.$emit('update:showdialog', false)
    },
    // 聚焦时展示树形结构-部门
    async treeShow() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.treeNode = await transListToTreeData(depts, '') // 递归算法
      await this.$sleep()
      this.loading = false
    },
    // 点击将部门数据放入输入框并关闭树形结构
    handleNodeClick(data) {
      // console.log(data) //data表示点击选中的数据对象
      this.adeptForm.departmentName = data.name
      this.showTree = false
    },
    // 添加员工
    async addemployee() {
      try { // 用trycatch处理数据请求,使代码更具有鲁棒性,测试性能好
        await addemployee(this.adeptForm)
        this.$parent.showdialog = false// 由于@close事件,关闭弹窗时自动触发清空表单
        this.$parent.getemployeeList() // 利用父级初始化员工列表
        this.$message.success('添加员工成功!')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style>

</style>
